
$(function(){
    var lightenBtn = $('#lighten');//点亮灯笼按钮
    var indexBox = $('#index-box');//首页界面
    var countdownBox = $('#countdown-box');//倒计时界面
    var overBox = $('#overBox');//结束界面
    var state = $('.state'); //状态
    var startBox = $('#startGame');
    var light = 'img/light.png'; //点亮
    var nolight = 'img/nolight.png'; //未点亮
    var gameGlim = $('#game-glim img'); //灯笼
    var imgParent = $('#game-glim');
    var arrGlim = [];
    var gameStartTime;
    var stime;
//    界面切换
    lightenBtn.on('click',function(){
        indexBox.addClass('glim-hide');
        indexBox.removeClass('glim-show');
        countdownBox.addClass('glim-show');
        countdownBox.removeClass('glim-hide');
        countDown();//游戏开始倒计时执行
    });
//    倒计时    
    var countTime = 4;
    var countDown = function(){
        var t = setTimeout(countDown,1000);
        if(countTime > 0){
            countTime -- ;
            $('#count-time').html(countTime);
        }else{
            countdownBox.removeClass('glim-show');
            countdownBox.addClass('glim-hide');
            startBox.removeClass('glim-hide');
            startBox.addClass('glim-show');
            clearTimeout(t); // 清除倒计时
            gameTimeShow(); // 游戏倒计时执行
            gameStartTime =  setInterval(gameStart,10);
        }
    }
//    游戏开始
    var gameState = true;
    var gameTime = 61;
    var gameTimeShow = function(){
        console.log(988777)
        stime = setTimeout(gameTimeShow,1000);
        if(gameTime > 0){
            gameTime --;
            $('#game-time').html(gameTime);
          
        }else{
            clearTimeout(stime);
            clearTimeout(gameStart);
            startBox.removeClass('glim-show');
            startBox.addClass('glim-hide');
            overBox.removeClass('glim-hide');
            overBox.addClass('glim-show');
            imgParent.empty();
            console.log('游戏结束');
        }
    }
//    灯笼图片
    var num = 0;
    var gameStart = function(){ 
        num ++;
        console.log(num)
        if(num == 100){
            var glimWidth = 110;
            var src = 'img/nolight.png';
            var glimLeft = Math.random() * (750 - glimWidth)/100;
            arrGlim.push(new FoundGlim(0,glimLeft.toFixed(2),imgParent,src));
            num = 0;
         }
         for(var i =0; i < arrGlim.length; i ++){
            arrGlim[i].move();
            if(parseInt(arrGlim[i].node.css('bottom')) >= $(window).height() - 70){
                arrGlim[i].node.remove();
                arrGlim.splice(i,1);
                i --;
            }
         }
         if(gameTime <=0){
             clearInterval(gameStartTime)
         }
    }
    var glimNum = 0;
    var gameNum = 0;
    var FoundGlim = function(bottom,left,parent,src){
        glimNum += 0.01;
        var nums = glimNum.toFixed(2);
        this.bottom = bottom;
        this.position = "absolute";
        this.left = left;
        this.parent = parent;
        this.src = src;
        this.node = null;
        this.init = function (){
            this.node = $('<img>');
            this.node.css('position',this.position);
            this.node.attr('src',this.src);
            this.node.css('bottom',this.bottom + 'rem');
            this.node.css('left',this.left + 'rem');
            this.parent.append(this.node);
        }
        this.init();
        var _this = this;
      
          this.node.on('click',function(e){
            _this.node.attr('src','img/light.png');
            gameNum ++;
            $('#game-num').html(gameNum);
            $('#overNum').html(gameNum);
          });
    
        this.move = function(){
            this.node.css('bottom',parseInt(this.node.css('bottom')) + 2 + "px");
        }
    }
    
   $('.recur').on('click',function(){
       overBox.addClass('glim-hide');
       overBox.removeClass('glim-show');
       startBox.addClass('glim-show');
       startBox.removeClass('glim-hide');
       $('#game-time').html(60);
       $('#game-num').html(0);
       gameStartTime =  setInterval(gameStart,10);
       stime = setTimeout(gameTimeShow,1000);
       gameNum = 0;
       gameTime = 61;
       
   });
   
    
    
    

    
    
    
    

});


